package com.zhuchaooasystem.ui.setting;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhuchaooasystem.R;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.entity.ChangePWEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.presenter.setting.ChangePasswordContract;
import com.zhuchaooasystem.presenter.setting.ChangePasswordPresenter;
import com.zhuchaooasystem.utils.LiteDbUtils;
import com.zhuchaooasystem.widget.StateButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseActivity<ChangePasswordPresenter>
        implements ChangePasswordContract.View {

    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_txt_title)
    TextView actionbarTxtTitle;
    @BindView(R.id.act_change_pw_et_old)
    EditText actChangePwEtOld;
    @BindView(R.id.act_change_pw_et_new)
    EditText actChangePwEtNew;
    @BindView(R.id.act_change_pw_et_new_again)
    EditText actChangePwEtNewAgain;
    @BindView(R.id.act_change_pw_btn)
    StateButton actChangePwBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_change_password;
    }

    @Override
    protected ChangePasswordPresenter getPresenter() {
        return new ChangePasswordPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initView() {
        actionbarIvBack.setVisibility(View.VISIBLE);
        actionbarTxtTitle.setText("更改密码");

        //设置EditView焦点获取
        actChangePwEtOld.setFocusable(false);

        actChangePwEtOld.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                actChangePwEtOld.setFocusableInTouchMode(true);

                return false;
            }
        });

        actChangePwEtNew.setFocusable(false);

        actChangePwEtNew.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                actChangePwEtNew.setFocusableInTouchMode(true);

                return false;
            }
        });
        actChangePwEtNewAgain.setFocusable(false);

        actChangePwEtNewAgain.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                actChangePwEtNewAgain.setFocusableInTouchMode(true);

                return false;
            }
        });
    }

    @Override
    protected void initDataAndLoadData() {

    }

    @OnClick({R.id.actionbar_iv_back, R.id.act_change_pw_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                ChangePasswordActivity.this.finish();
                break;
            case R.id.act_change_pw_btn:
                mPresenter.changePassword();
                break;
        }
    }

    @Override
    public String getOldPassword() {
        return actChangePwEtOld.getText().toString();
    }

    @Override
    public String getnewPassword() {
        return actChangePwEtNew.getText().toString();
    }

    @Override
    public String getnewPasswordAgain() {
        return actChangePwEtNewAgain.getText().toString();
    }

    /**
     * 修改密码返回结果
     *
     * @param entity
     */
    @Override
    public void getResult(ChangePWEntity entity, String password) {

        if (entity.getResult().getSuccessful() != null &&
                !(entity.getResult().getSuccessful().equals(""))) {

            Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();

            //需要更新数据里面的密码
            UserInfo userInfo = UserInfo.getUserInfo();

            if (userInfo != null) {

                //更改密码成功以后  更新数据库里的密码
                userInfo.setPassword(password);

                userInfo.updataIncome(userInfo);

            }


            ChangePasswordActivity.this.finish();

        } else if (entity.getResult().getError() != null &&
                !(entity.getResult().getError().equals(""))) {

            Toast.makeText(mContext, "修改失败", Toast.LENGTH_SHORT).show();


        }
    }

    @Override
    public void setLoginClick(boolean canClick) {
        if (canClick) {
            actChangePwBtn.setClickable(true);
        } else {
            actChangePwBtn.setClickable(false);
        }
    }
}
